package utils;

/**
 * 文件名加密/解密类<br>
 * B为二进制，D为十进制，H为十六进制
 * 
 * @author jungle
 *
 */
public class FCFileNameDE {

	private char[] hex = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };

	protected FCFileNameDE() {

	}

	/**
	 * 文件名加密
	 * 
	 * @param filename
	 * @return
	 */
	public String filenameEncryption(String filename) {
		String newname = "";
		for (int i = 0; i < filename.length(); i++) {
			newname += DectoHex(filename.charAt(i));
		}
		return newname;
	}

	/**
	 * 文件名解密
	 * 
	 * @param filename
	 * @return
	 */
	public String filenameDecrypt(String filename) {
		String newname = "";
		for (int i = 0; i < filename.length() / 2; i++) {
			newname += HextoChar(filename.substring(i * 2, i * 2 + 2));
		}
		return newname;
	}

	/**
	 * 二进制转16进制
	 * 
	 * @param num
	 * @return
	 */
	private String DectoHex(int num) {
		String qw = "";
		while (true) {
			qw = hex[num % 16] + qw;
			if (num > 15) {
				num = num / 16;
			} else {
				break;
			}
		}
		if (qw.length() < 2) {
			return "0" + qw;
		} else {
			return qw;
		}
	}

	/**
	 * 16进制转字符类型
	 * 
	 * @param h
	 * @return
	 */
	private char HextoChar(String h) {
		return (char) (ChartoNum(h.charAt(0)) * 16 + ChartoNum(h.charAt(1)));
	}

	/**
	 * 字符转数字
	 * 
	 * @param a
	 * @return
	 */
	private int ChartoNum(char a) {
		for (int i = 0; i < 16; i++) {
			if (hex[i] == a) {
				return i;
			}
		}
		return 0;
	}
}
